<?php

namespace app\admin\controller\general;

use app\common\controller\Backend;
use fast\Random;
use think\Db;
use think\Validate;

/**
 * 友情链接管理
 *
 */
class Links extends Backend
{

    /**
     * @var \app\admin\model\Links
     */
    protected $model = null;
    protected $selectpageFields = 'id,name';
    protected $searchFields = 'name';
    protected $childrenGroupIds = [];
    protected $childrenAdminIds = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Links');

        $position_list = [
            "1" => "首页",
            "2" => "图书馆",
        ];
        $this->assign("position_list", $position_list);
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                try {

                    $result = $this->model->validate('Links.add')->save($params);
                    if ($result === false) {
                        exception($this->model->getError());
                    }
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {

                try {
                    $result = $row->validate('Links.edit')->save($params);
                    if ($result === false) {
                        exception($row->getError());
                    }
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $deleteIds = explode(",", $ids);
            try {
                $this->model->destroy($deleteIds);
                $this->success();
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->error(__('No rows were deleted'));
        }
    }

    /**
     * 批量更新
     * @internal
     */
    public function multi($ids = "")
    {
        // 管理员禁止批量操作
        $this->error();
    }

    /**
     * 下拉搜索
     */
    public function selectpage()
    {
        $this->dataLimit = 'auth';
        $this->dataLimitField = 'id';
        return parent::selectpage();
    }
}
